package com.suypower.carManage.json;

import com.jarp.db.DAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.suypower.carManage.VO.TreeVO;
import com.suypower.carManage.comm.ConstUtil;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import java.util.*;

/**
 * Created by zhaoyuan on 14-2-17.
 */
public class MenuListJson extends ActionSupport {
    ActionContext cxt = ActionContext.getContext();
    Map<String, Object> session = cxt.getSession();
    String userid = (String) session.get("userid");
    DAO dao = new DAO();

    public String execute() {
        System.out.println("获取菜单列表");
        String res = "";
       res= getDataList("-1");
        ActionContext.getContext().put("data", res);
        return SUCCESS;
    }

    public String getDataList(String parentid) {
        String res = "";
        List<TreeVO> lstResults = new ArrayList<TreeVO>();
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT t.*,a.leavel FROM tb_access t ");
        sql.append("LEFT JOIN tb_role_access a ON a.accessid=t.id ");
        sql.append("LEFT JOIN tb_role b ON b.id=a.roleid ");
        sql.append("LEFT JOIN tb_user_role c ON c.roleid=b.id ");
        sql.append("LEFT JOIN tb_user d ON c.userid=d.userid ");
        sql.append("WHERE d.userid=").append(userid);
        sql.append(" and t.pid=").append(parentid);
        sql.append(" ORDER BY t.ord ASC ");
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME,sql.toString());
            if(v!=null){
                for(int i=0;i<v.size();i++){
                    TreeVO tree=new TreeVO();
                    Hashtable hashtable=(Hashtable)v.get(i);
                    tree.setText((String)hashtable.get("NAME"));
                    tree.setId((String)hashtable.get("ID"));
                    tree.setUrl((String)hashtable.get("URL"));
                    tree.setAccess((String)hashtable.get("LEAVEL"));
                    boolean leaf=this.isLeaf(tree.getId());
                    tree.setLeaf(leaf);
                    if(!leaf){
                        tree.setChildren(getData(tree.getId()));
                    }
                    lstResults.add(tree);
                }
            }
        } catch (ServletException e) {
            e.printStackTrace();
        }

        JSONArray json = new JSONArray();
        json = JSONArray.fromObject(lstResults);
        res = json.toString();
        return res;
    }
    /**
     * 获取用户的权限列表
     * @param parentid
     * @return
     */
    public String getData(String parentid) {
        String res = "";
        List<TreeVO> lstResults = new ArrayList<TreeVO>();
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT t.*,a.leavel FROM tb_access t ");
        sql.append("LEFT JOIN tb_role_access a ON a.accessid=t.id ");
        sql.append("LEFT JOIN tb_role b ON b.id=a.roleid ");
        sql.append("LEFT JOIN tb_user_role c ON c.roleid=b.id ");
        sql.append("LEFT JOIN tb_user d ON c.userid=d.userid ");
        sql.append("WHERE d.userid=").append(userid);
        sql.append(" and t.pid=").append(parentid);
        sql.append(" ORDER BY t.ord ASC ");
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME,sql.toString());
            if(v!=null){
                for(int i=0;i<v.size();i++){
                    TreeVO tree=new TreeVO();
                    Hashtable hashtable=(Hashtable)v.get(i);
                    tree.setText((String)hashtable.get("NAME"));
                    tree.setId((String)hashtable.get("ID"));
                    tree.setUrl((String)hashtable.get("URL"));
                    tree.setAccess((String)hashtable.get("LEAVEL"));
                    boolean leaf=this.isLeaf(tree.getId());
                    tree.setLeaf(leaf);
                    if(!leaf){
                        tree.setChildren(getData(tree.getId()));
                    }
                    lstResults.add(tree);
                }
            }
        } catch (ServletException e) {
            e.printStackTrace();
        }

        JSONArray json = new JSONArray();
        json = JSONArray.fromObject(lstResults);
        res = json.toString();
        return res;
    }

    public Boolean isLeaf(String ID) {
        try {

            StringBuffer sql = new StringBuffer();
            sql.append("SELECT t.* FROM tb_access t ");
            sql.append("LEFT JOIN tb_role_access a ON a.accessid=t.id ");
            sql.append("LEFT JOIN tb_role b ON b.id=a.roleid ");
            sql.append("LEFT JOIN tb_user_role c ON c.roleid=b.id ");
            sql.append("LEFT JOIN tb_user d ON c.userid=d.userid ");
            sql.append("WHERE d.userid=").append(userid);
            sql.append(" and t.pid=").append(ID);
            sql.append(" ORDER BY t.ord ASC ");
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sql.toString());
            if (v == null) {
                return true;
            } else {
                return false;
            }
        } catch (ServletException ex) {
            return false;
        }
    }


}
